package org.rabbitmq.test3;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.rabbitmq.utils.RabbitMqUtils;
import org.rabbitmq.utils.SleepUtils;

public class Work03 {

    public static final String TASK_QUEUE_NAME = "task";
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        //声明 接收消息
        DeliverCallback deliverCallback = (consumerTag, message) ->{
            //沉睡1s
            SleepUtils.sleep(1);
            System.out.println("工作3"+new String(message.getBody(),"UTF-8"));
            //手动应答
            /**
             * 1.消息标记tag
             * 2.是否批量应答
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };

        //取消消息时的回调
        CancelCallback cancelCallback = consumerTag ->{
            System.out.println("消息消费被中断");
        };
        System.out.println("C3接收时间短");
        //设置不公平分发
        channel.basicQos(2);
        //手动应答
        boolean autoAck = false;
        //接收消息
        channel.basicConsume(TASK_QUEUE_NAME,autoAck,deliverCallback,cancelCallback);
    }
}
